package cc.picc.quota;

import org.apache.commons.lang3.RandomStringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

/**
 * 使用String作为令牌的桶
 * 
 * @author lijinting01
 *
 */
public class StringQuotaBucket extends AbstractQuotaBucket<String> {

	private static final int BOUNDED_SIZE = 10000;

	/**
	 * 
	 */
	public StringQuotaBucket() {
		this(BOUNDED_SIZE);
	}

	public StringQuotaBucket(int capacity) {
		super(capacity);
	}

	@Override
	protected QuotaProvider<String> defineQuotaProvider() {
		return this::newQuotaString;
	}

	private String newQuotaString() {
		final String dateTime = DateTimeFormat.forPattern("yyyyMMddHHmmss").print(DateTime.now());
		final String randomNumber = RandomStringUtils.randomNumeric(16);
		return String.format("%s-%s", dateTime, randomNumber);
	}
}
